package jacksen.cluster;

import android.content.Context;
import android.graphics.Point;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.TextView;

import com.amap.api.maps.model.BitmapDescriptor;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jacksen on 2015/12/4.
 */
public class MarkersCluster {

    private Point point;
    private LatLng latLng;
    private List<ClusterInter> clusterInters;
    private Marker marker;

    /**
     * @param point
     * @param latLng
     */
    public MarkersCluster(Point point, LatLng latLng) {
        this.point = point;
        this.latLng = latLng;
        clusterInters = new ArrayList<>();
    }

    /**
     * @param clusterInter
     */
    public void addClusterItem(ClusterInter clusterInter) {
        clusterInters.add(clusterInter);
    }

    /**
     * @return
     */
    private int getClusterCount() {
        return clusterInters.size();
    }

    /**
     * @return
     */
    public Point getCenterPoint() {
        for (ClusterInter clusterInter: clusterInters) {
        }
        return point;
    }

    /**
     * @return
     */
    private LatLng getCenterLatLng() {
        return latLng;
    }

    /**
     * @param marker
     */
    public void setMarker(Marker marker) {
        this.marker = marker;
    }

    /**
     * @return
     */
    public Marker getMarker() {
        return marker;
    }

    /**
     *
     * @param context
     * @return
     */
    public MarkerOptions getMarkerOptions(Context context) {
        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.position(getCenterLatLng());
        markerOptions.anchor(0.5f, 0.5f);
        markerOptions.icon(getBitmapDescriptor(context, clusterInters.size()));
        return markerOptions;
    }

    /**
     * @return
     */
    public List<ClusterInter> getClusterInters() {
        return clusterInters;
    }

    /**
     * @return
     */
    public BitmapDescriptor getBitmapDescriptor(Context context, int num) {
        TextView textView = new TextView(context);
        if (clusterInters.size() > 1) {
            textView.setText(String.valueOf(clusterInters.size()));
        }
        textView.setGravity(Gravity.CENTER);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
        textView.setBackgroundResource(R.drawable.marker_red);
        return BitmapDescriptorFactory.fromView(textView);
    }
}
